package com.fantastic.service.zjs.impl;

import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.common.exception.sql.DeleteException;
import com.fantastic.common.exception.sql.InsertException;
import com.fantastic.common.exception.sql.SelectException;
import com.fantastic.common.exception.sql.UpdateException;
import com.fantastic.common.utils.IsEmptyUtils;
import com.fantastic.common.utils.MultipartFileUtils;
import com.fantastic.mapper.zjs.SpecMapper;
import com.fantastic.model.pojo.zjs.Spec;
import com.fantastic.service.zjs.SpecService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: rxx
 * @since: 2024/5/31 09:45
 * @description: 菜品规格信息service接口
 */
@Service
public class SpecServiceImpl implements SpecService {

    @Resource
    private SpecMapper specMapper;

    /**
     * 查询
     * 无条件时是查询全部的分页查询
     * 有条件时是查询条件的分页查询
     *
     * @param spec
     * @return
     */
    @Override
    public List<Spec> listSpec(Spec spec) {
        List<Spec> specs = specMapper.listSpec(spec);
        IsEmptyUtils.listIsEmpty(new SelectException(RespStatusInfo.SELECT_NULL), specs);
        return specs;
    }

    /**
     * 查询单个菜品规格信息
     *
     * @param specId 菜品规格信息id
     * @return
     */
    @Override
    public Spec getSpec(Integer specId) {
        IsEmptyUtils.integerIsEmpty(new SelectException(RespStatusInfo.SELECT_ERROR), specId);
        return specMapper.getSpec(specId);
    }

    /**
     * 添加菜品规格信息
     *
     * @param spec     菜品规格信息对象
     * @param specFile 规格文件
     * @return
     */
    @Override
    public Integer insertSpec(MultipartFile specFile, Spec spec) {
        IsEmptyUtils.integerIsEmpty(new InsertException(RespStatusInfo.INSERT_ERROR), spec.getSpecTypeId());
        IsEmptyUtils.stringIsEmpty(new InsertException(RespStatusInfo.INSERT_ERROR), spec.getSpecName());
        if (specFile != null && specFile.getSize() != 0) {
            MultipartFileUtils.supportType(specFile.getOriginalFilename());
            String relativePath = MultipartFileUtils.writeFile(specFile, "菜品");
            spec.setSpecPicture(relativePath);
        } else {
            throw new InsertException(RespStatusInfo.FILETYPE_NULL);
        }
        return specMapper.insertSpec(spec);
    }

    /**
     * 修改菜品规格信息
     *
     * @param specFile 规格文件
     * @param spec
     * @return
     */
    @Override
    public Integer updateSpec(MultipartFile specFile, Spec spec) {
        IsEmptyUtils.integerIsEmpty(new UpdateException(RespStatusInfo.UPDATE_ERROR), spec.getSpecId());
        if (specFile != null && specFile.getSize() != 0) {
            MultipartFileUtils.supportType(specFile.getOriginalFilename());
            String relativePath = MultipartFileUtils.writeFile(specFile, "菜品");
            spec.setSpecPicture(relativePath);
            //查询原来的图片
            Spec getSpec = getSpec(spec.getSpecId());
            //删除图片
            MultipartFileUtils.deleteFile(getSpec.getSpecPicture());
        }
        return specMapper.updateSpec(spec);
    }

    /**
     * 删除菜品规格信息
     *
     * @param specId
     * @return
     */
    @Override
    public Integer deleteSpec(Integer specId) {
        IsEmptyUtils.integerIsEmpty(new DeleteException(RespStatusInfo.DELETE_ERROR), specId);
        return specMapper.deleteSpec(specId);
    }
}
